//
//  VistorLoginView.swift
//  wei
//
//  Created by 江齐松 on 15/7/29.
//  Copyright © 2015年 江齐松. All rights reserved.
//

import UIKit

protocol visitlogindelegate:NSObjectProtocol{
    func login()
}

class VistorLoginView: UIView {

   weak var delegate: visitlogindelegate?//没有问号会报错
    
    func clicklogin(){
        self.delegate?.login()
        print("zhuce")
        
    }
    
    
    
    
    func setupinfo(ishome: Bool,imagename:String,message:String){
        
        messagrLabel.text = message
        smView.image = UIImage(named: imagename)
        iconView.hidden = !ishome
        
//        if !ishome{
//            sendSubviewToBack(kView)
//        }
        
        ishome ? zhuan() : sendSubviewToBack(kView)
    }
    
    func zhuan(){
        let anim = CABasicAnimation(keyPath: "transform.rotation")
        
        anim.toValue = 2 * M_PI
        anim.repeatCount = MAXFLOAT
        anim.duration = 20.0
        anim.removedOnCompletion = false
        
        smView.layer.addAnimation(anim, forKey: nil)

    }
    
    override init(frame: CGRect){
        super.init(frame: frame)
        setupUI()

    }

    required init?(coder aDecoder: NSCoder) {
        //fatalError("init(coder:) has not been implemented")
    
        super.init(coder: aDecoder)
        setupUI()
    }
    
    //设置UI界面
    private func setupUI(){
        
        addSubview(smView)
        addSubview(kView)
        addSubview(iconView)

//        addSubview(messageView)
//        addSubview(profileView)
        addSubview(registerButton)
        addSubview(cancellButton)
        addSubview(messagrLabel)
        
        
        iconView.translatesAutoresizingMaskIntoConstraints = false
        addConstraint(NSLayoutConstraint(item: iconView, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.CenterX, multiplier: 1.0, constant: 0))
        addConstraint(NSLayoutConstraint(item: iconView, attribute: NSLayoutAttribute.CenterY, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.CenterY, multiplier: 1.0, constant: -40))
        
        smView.translatesAutoresizingMaskIntoConstraints = false
        addConstraint(NSLayoutConstraint(item: smView, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: iconView, attribute: NSLayoutAttribute.CenterX, multiplier: 1.0, constant: 0))
        addConstraint(NSLayoutConstraint(item: smView, attribute: NSLayoutAttribute.CenterY, relatedBy: NSLayoutRelation.Equal, toItem: iconView, attribute: NSLayoutAttribute.CenterY, multiplier: 1.0, constant: 0))
        
        
        messagrLabel.translatesAutoresizingMaskIntoConstraints = false
        addConstraint(NSLayoutConstraint(item: messagrLabel, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: iconView, attribute: NSLayoutAttribute.CenterX, multiplier: 1.0, constant: 0))
        addConstraint(NSLayoutConstraint(item: messagrLabel, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: iconView, attribute: NSLayoutAttribute.Bottom, multiplier: 1.0, constant: 20))
        addConstraint(NSLayoutConstraint(item: messagrLabel, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1.0, constant: 224))
        
        registerButton.translatesAutoresizingMaskIntoConstraints = false
        addConstraint(NSLayoutConstraint(item: registerButton, attribute: NSLayoutAttribute.Left, relatedBy: NSLayoutRelation.Equal, toItem: messagrLabel, attribute: NSLayoutAttribute.Left, multiplier: 1.0, constant: 0))
        addConstraint(NSLayoutConstraint(item: registerButton, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: iconView, attribute: NSLayoutAttribute.Bottom, multiplier: 1.0, constant: 50))
        addConstraint(NSLayoutConstraint(item: registerButton, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1.0, constant: 170))
        addConstraint(NSLayoutConstraint(item: registerButton, attribute: NSLayoutAttribute.Height, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1.0, constant: 30))
        
        kView.translatesAutoresizingMaskIntoConstraints = false
        addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("H:|-0-[subview]-0-|", options: NSLayoutFormatOptions(rawValue: 0), metrics: nil, views: ["subview" : kView]))
        addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("V:|-0-[subview]-160-|", options: NSLayoutFormatOptions(rawValue: 0), metrics: nil, views: ["subview" : kView]))
        
        
        backgroundColor = UIColor(white: 237.0 / 255.0, alpha: 1.0)

    }
    
    private lazy var smView: UIImageView = {
        let iv = UIImageView(image: UIImage(named: "visitordiscover_feed_image_smallicon"))
        return iv
        }()
    


    
    private lazy var iconView: UIImageView = {
        let iv = UIImageView(image: UIImage(named: "visitordiscover_feed_image_house"))
        return iv
    }()
    
//    private lazy var maskView: UIImageView = {
//        let ms = UIImageView(image: UIImage(named: "visitordiscover_feed_mask_smallicon"))
//        return ms
//    }
    
    private lazy var messageView: UIImageView = {
        let ivm = UIImageView(image: UIImage(named: "visitordiscover_image_message"))
        return ivm
        }()
    
    private lazy var profileView: UIImageView = {
        let ivp = UIImageView(image: UIImage(named: "visitordiscover_image_profile"))
        return ivp
        }()
    
    private lazy var kView: UIImageView = {
        let ivm = UIImageView(image: UIImage(named: "visitordiscover_feed_mask_smallicon"))
        return ivm
        }()


    
    private lazy var messagrLabel: UILabel = {
        let label = UILabel()
        label.text = "睡你妈比起来嗨"
        label.textColor = UIColor.darkGrayColor()
        label.font = UIFont.systemFontOfSize(14)
        label.numberOfLines = 0
        
        label.sizeToFit()
        return label
    }()
    
    //注册按钮
    private lazy var registerButton: UIButton = {
        let btn = UIButton()//这样是自定义类型
        btn.setTitle("注册", forState: UIControlState.Normal)
        btn.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: UIControlState.Normal)
        btn.setTitleColor(UIColor.orangeColor(), forState: UIControlState.Normal)
        
        btn.addTarget(self, action: "clicklogin", forControlEvents: UIControlEvents.TouchUpInside)
        return btn
        
    }()

    private lazy var cancellButton: UIButton = {
        let btn = UIButton()//这样是自定义类型
        btn.setTitle("登陆", forState: UIControlState.Normal)
        btn.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: UIControlState.Normal)
        return btn
        
    }()





}
